package com.developer.fans;

import android.content.Context;
import android.util.Log;

import java.io.File;

/**
 * Created by sunny on 15-8-15.
 */
public class CleanCache {
    Context mContext;

    public CleanCache(Context context) {
        mContext = context;
    }

    private void getCacheFile() {
        String pkg = mContext.getPackageName() + "/files";
        String dataPath = mContext.getFilesDir().getAbsolutePath();
        String data = dataPath.substring(0, dataPath.length() - pkg.length());
        String dataAppStore = data + "com.tianci.appstore";
        Log.i("", "sunny data: " + dataPath + "; " + pkg + "; " + data + "; " + dataPath.length() + "; " + pkg.length() + "; " + dataAppStore);
        File file = new File(dataAppStore);
        RecursionDeleteFile(file);
    }

    public void RecursionDeleteFile(File file) {
        if (!file.exists()) {
//            Toast.makeText(this, "没有缓存！", Toast.LENGTH_SHORT).show();
        } else {
            if (file.isFile()) {
                file.delete();
                return;
            }
            if (file.isDirectory()) {
                File[] childFile = file.listFiles();
                if (childFile == null || childFile.length == 0) {
                    file.delete();
                    return;
                }
                for (File f : childFile) {
                    RecursionDeleteFile(f);
                }
                file.delete();
            }
//            Toast.makeText(this, "删除缓存成功！", Toast.LENGTH_SHORT).show();
        }
    }
}
